/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.gda.pg;

import com.mongodb.BasicDBObject;
import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;

/**
 *
 * @author M. Zawadzki
 */
public class DBUtils {
    private static final String DB_NAME = "test";
    private static final String COLL_NAME = "czlowieki";

    private static MongoClient mongoClient;
    private static DB database;
    private static DBCollection collection;
    private static ArrayList<ObjectId> entryId = new ArrayList();

    /**
     * Connects to database at localhost
     * 
     * @throws UnknownHostException
     */
    public static void connect() throws UnknownHostException {
        mongoClient = new MongoClient();
        database = mongoClient.getDB(DB_NAME);
        collection = database.getCollection(COLL_NAME);
    }

    /**
     * Frees resources and removes references to client and database
     */
    public static void disconnect() {
        mongoClient = null;
        database = null;
    }
    
    public static List<DBObject> getAllEntries() {
        DBCursor cursor = collection.find();
        
        return fillEntriesList(cursor);
    }
    
    public static List<DBObject> fillEntriesList(DBCursor cursor) {
        List<DBObject> entries = new ArrayList<>(); 
        
        try {
           while(cursor.hasNext()) {
               entries.add(cursor.next());
           }
        } finally {
           cursor.close();
        }
        
        return entries;
    }
    
    public static int createEntry(String imie) {
        String nazwisko = TextUtils.readLine("nazwisko");
        String telefonSluzbowy = TextUtils.readLine("telefon sluzbowy");
        String telefonDomowy = TextUtils.readLine("telefon domowy");
        String mailSluzbowy = TextUtils.readLine("mail sluzbowy");
        String mailPrywatny = TextUtils.readLine("mail prywatny");
        String adresSluzbowyUlica = TextUtils.readLine("ulica");
        String adresSluzbowyDom = TextUtils.readLine("nr domu");
        String adresSluzbowyLokal = TextUtils.readLine("nr mieszkania");
        String adresSluzbowyKod = TextUtils.readLine("kod pocztowy");
        String adresSluzbowyMiasto = TextUtils.readLine("miasto");
        String zwierz = TextUtils.readLine("zwierze");
        
        try {
            Integer lokal = Integer.parseInt(adresSluzbowyLokal);
        } catch (NumberFormatException e) {
            System.out.println("Lokal musi być liczbą");
            return -1;
        }
        
        if (imie.equals("") || nazwisko.equals(""))
            return -1;
        
        BasicDBObject czlowiek = new BasicDBObject();
        czlowiek.append("imie", imie);
        czlowiek.append("nazwisko", nazwisko);
        
        czlowiek.append("telefony", new BasicDBObject("sluzbowy", telefonSluzbowy).append("domowy", telefonDomowy));
        
        czlowiek.append("maile", new BasicDBObject("sluzbowy", mailSluzbowy).append("prywatny", mailPrywatny));
        
        czlowiek.append("adresy", new BasicDBObject("firmowy", new BasicDBObject("ulica", adresSluzbowyUlica)
                .append("nrdomu", adresSluzbowyDom)
                .append("nrmieszkania", Integer.parseInt(adresSluzbowyLokal))
                .append("kod", adresSluzbowyKod)
                .append("miasto", adresSluzbowyMiasto)));
        
        if( zwierz.equalsIgnoreCase("true") || zwierz.equalsIgnoreCase("false"))
            czlowiek.append("zwierz", Boolean.getBoolean(zwierz));
        else{
            System.out.println("Zwierz musi być wartością true lub false.");
            return -1;
        }
        
       
        collection.insert(czlowiek);
        return 0;
    }
    
    public static void deleteEntry(String imie){
        
        BasicDBObject doc = new BasicDBObject();
        
        doc.append("imie", imie);
        
        collection.remove(doc);
    }
    
    public static List<DBObject> getEntriesByName(String name) {
        BasicDBObject query = new BasicDBObject("imie", name);
        
        DBCursor cursor = collection.find(query);
        
        return fillEntriesList(cursor);
    }
    
    public static List<DBObject> getEntriesByCity(String city) {
        BasicDBObject query = new BasicDBObject("adresy", new BasicDBObject("firmowy", 
                new BasicDBObject("miasto", city)));
        
        System.out.println(query.toString());
        
        DBCursor cursor = collection.find(query);
        
        return fillEntriesList(cursor);
    }

    // Other methods

    public static void updateEntry() {
        String imie = TextUtils.readLine("imie");
        
        deleteEntry(imie);
        
        createEntry(imie); 
    }
}
